package com.hifar.mybatisplus.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hifar.mybatisplus.MasterPublicMapperService;
import com.hifar.mybatisplus.MetaField;
import com.hifar.mybatisplus.PublicMapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service
public class MasterPublicMapperServiceImpl implements MasterPublicMapperService  {

	@Autowired
	private PublicMapper publicMapper;

	@Override
	public Map<String, Object> selectOneBySql(String sql) {

		return publicMapper.selectOneBySql(sql);
	}
	@Override
	public <T,E> Page<T> listPage(Page<E> rowPage, String sql, QueryWrapper<E> wrapper,Class<T> classT) {

		Page<Map<String, Object>> page4 =new Page<Map<String, Object>>(rowPage.getCurrent(),rowPage.getSize());

		Page<Map<String, Object>> dataPage=publicMapper.listPage(page4, sql, wrapper);
		Page<T> pageObj=new Page<T>(dataPage.getCurrent(),dataPage.getSize());
		pageObj.setTotal(dataPage.getTotal());
		pageObj.setPages(dataPage.getPages());
		List<Map<String, Object>> MapList= dataPage.getRecords();
		List<T> tList=new ArrayList<T>();
		for(Map<String, Object> oneMap:MapList) {
			T oneBean=null;
			try {
				oneBean = classT.newInstance();

				BeanUtil.fillBeanWithMap(oneMap, oneBean, true);
			} catch (InstantiationException | IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			tList.add(oneBean);

		}
		pageObj.setRecords(tList);
		return pageObj;
	}
	@Override
	public Page<Map<String, Object>> listPage(Page<Map<String, Object>> rowPage, String sql, QueryWrapper wrapper) {
		return publicMapper.listPage(rowPage, sql, wrapper);
	}

	@Override
	public List<Map<String, Object>> selectList(String sql, QueryWrapper wrapper) {

		return publicMapper.selectList(sql, wrapper);
	}
	@Override
	public <T> List<T> selectList(@Param("sql") String sql, @Param(Constants.WRAPPER) QueryWrapper wrapper,Class<T> classT ){
		List<Map<String, Object>> MapList=  publicMapper.selectList(sql, wrapper);

		List<T> tList=new ArrayList<T>();
		for(Map<String, Object> oneMap:MapList) {
			T oneBean=null;
			try {
				oneBean = classT.newInstance();

				BeanUtil.fillBeanWithMap(oneMap, oneBean, true);
			} catch (InstantiationException | IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			tList.add(oneBean);

		}
		return tList;
	}

	@Override
	public Map<String, Object> selectOneById(String tableName, QueryWrapper wrapper) {

		return publicMapper.selectOneById(tableName, wrapper);
	}

	@Override
	public Map<String, Object> selectOneByIdField(String selectFields, String tableName, QueryWrapper wrapper) {

		return publicMapper.selectOneByIdField(selectFields, tableName, wrapper);
	}

	@Override
	public List<Map<String, Object>> selectListField(String selectFields, String tableName, QueryWrapper wrapper) {

		return publicMapper.selectListField(selectFields, tableName, wrapper);
	}

	@Override
	public void insert(String tableName, List<MetaField> list) {
		publicMapper.insert(tableName, list);

	}

	@Override
	public void update(String tableName, QueryWrapper wrapper, List<MetaField> list) {
		publicMapper.update(tableName, wrapper, list);

	}

	@Override
	public void delete(String tableName, QueryWrapper wrapper) {
		publicMapper.delete(tableName, wrapper);

	}

}
